package handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import packet.impl.LoginRequestAbstractPacket;
import packet.impl.LoginResponseAbstractPacket;
import utils.LoginUtil;

import java.util.Date;
import java.util.UUID;

/**
 * @author Admin
 */
public class LoginResponseHandler extends SimpleChannelInboundHandler<LoginResponseAbstractPacket> {

  @Override
  protected void channelRead0(ChannelHandlerContext ctx, LoginResponseAbstractPacket loginResponsePacket) {
    if (loginResponsePacket.getSuccess()) {
      LoginUtil.markAsLogin(ctx.channel());
      System.out.println(new Date() + "登陆成功");
    } else {
      System.out.println(new Date() + "登陆失败，原因：" + loginResponsePacket.getReason());
    }
  }

  @Override
  public void channelActive(ChannelHandlerContext ctx) {
    LoginRequestAbstractPacket loginRequestPacket = new LoginRequestAbstractPacket();
    loginRequestPacket.setUserId(UUID.randomUUID().toString());
    loginRequestPacket.setUserId("zy.jordan");
    loginRequestPacket.setPassword("693909");

    ctx.channel().writeAndFlush(loginRequestPacket);
  }
}
